package interact;

import interact.click.ClickAction;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PVector;
import remixlab.proscene.Camera;
import remixlab.proscene.MouseGrabber;
import remixlab.proscene.Scene;

/**
 * Button 2D. by Jean Pierre Charalambos.
 * 
 * Base class of "2d buttons" that shows how simple is to implement a
 * MouseGrabber which can enable complex mouse interactions.
 */

public class Button2D extends MouseGrabber {
	String myText;
	PFont myFont = null;
	int myWidth;
	int myHeight;
	PVector position;
	ClickAction action;

	Button2D(Scene scn, PVector p,ClickAction action) {
		this(scn, p, "", action);
	}

	Button2D(Scene scn, PVector p, String t, ClickAction action) {
		super(scn);
		position = p;
		myText = t;
		this.action = action;
		// 
		// textFont(myFont);
	}

	void setText(PApplet canvas) {
		canvas.textFont(myFont);
		myWidth = (int) (canvas.textWidth(myText));
		myHeight = (int) (canvas.textAscent() + canvas.textDescent());
	}

	public void draw(PApplet canvas) {
		if(myFont==null) myFont = canvas.loadFont("FreeSans-36.vlw");
		canvas.pushStyle();
		canvas.textAlign(canvas.CENTER);
		setText(canvas);
		if (grabsMouse())
			canvas.fill(255);
		else
			canvas.fill(100);
		scene.beginScreenDrawing();
		canvas.text(myText, position.x, position.y, myWidth, myHeight);
		scene.endScreenDrawing();
		canvas.popStyle();
	}

	public void checkIfGrabsMouse(int x, int y, Camera camera) {
		// Rectangular activation area
		setGrabsMouse((position.x <= x) && (x <= position.x + myWidth)
				&& (position.y <= y) && (y <= position.y + myHeight));
	}
	
	@Override
	 public void mouseClicked(Integer button, int numberOfClicks, Camera camera) {
		 action.click(button, numberOfClicks, camera);
	 }
}